<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="d" uri="http://claudiushauptmann.com/jsp-decorator/"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<d:decorate filename='../shared/user-masterpage.jsp'>
    <d:content placeholder='title'>Afeka Bid System :: ${productModel.name}</d:content>
    <d:content placeholder='head'>
    </d:content>
    <d:content placeholder='body'>
        <table class="table-sales">
            <tr><td>Product:</td><td>${productModel.name}</td></tr>
            <tr><td>Type:</td><td>
            <c:choose>
                <c:when test="${productModel.type == 1}">
                    Tender
                </c:when>
                <c:otherwise>
                    Auction
                </c:otherwise>
            </c:choose>
            </td></tr>
            <tr><td>Description:</td><td>${productModel.description}</td></tr>
            <tr><td>Start Price:</td><td>$${productModel.price}</td></tr>
            <c:if test="${productModel.type == 1}">
                <tr><td>Close in:</td><td>${productModel.dateEnd}</td></tr>
            </c:if>
        </table>

        <c:if test="${productModel.bidAvailable}">
            <h2 style="text-align:left; direction:ltr; padding-top:20px; margin:0; margin-left:90px;">Place Bid:</h2>
            <div style="width:600px;margin:0 auto;">
                <input type="number" id="bid" name="bid" min="${productModel.minBid}"  />
                <input id="submit" type="button" value="" name="submit">
                <script type="text/javascript">
                    var productId = ${productModel.id};
                    $(document).ready(function(){
                        $("#submit").click(function(){
                            var bid = parseFloat($("#bid").val());
                            if(bid > 0){
                                $.post("/home/bid", {productId: productId, price: bid}, function(r){
                                    alert(r.string);
                                    location.reload();
                                });
                            }
                        });
                    });
                </script>
            </div>
        </c:if>

        <c:if test="${productModel.bids.size() > 0}">
            <h2 style="text-align:left; direction:ltr; padding-top:20px; margin:0; margin-left:90px;">Bid History:</h2>
            <div>
                <table class="table-sales">
                    <c:forEach items="${productModel.bids}" var="bid">
                    <tr>
                        <td>${bid.userName}</td>
                        <td>${bid.dateCreated}</td>
                        <td>$${bid.price}</td>
                    </tr>
                    </c:forEach>
                </table>
            </div>
        </c:if>
    </d:content>
</d:decorate>